import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 登录框的状态
    isOpenLogin: false,
    // 提示框的状态
    toast: {
      show: false,
      content: "你好",
      icon: "icon-toast_chenggong"
    }
  },
  getters: {
  },
  mutations: {
    // 登录框修改状态的方法
    changeIsLogin(state, payload) {
      state.isOpenLogin = payload
    },
    // 打开提示组件Toast
    openToast(state, payload) {
      state.toast.show = true,
        state.toast.content = payload.content;
      switch (payload.icon) {
        case "info":
          state.toast.icon = "icon-toast-jinggao";
          break;
        case "success":
          state.toast.icon = "icon-toast_chenggong";
          break;
        case "danger":
          state.toast.icon = "icon-toast-shibai_huaban";
          break;
        case "loading":
          state.toast.icon = "icon-loading";
          break;
        default:
          state.toast.icon = "icon-toast-jinggao";
          break;
      }
    },
    // 同步关闭toast
    closeToast(state) {
      state.toast.show = false
    },
  },
  actions: {
    AsyncToast({
      commit
    }, payload) {
      console.log(1);
      commit('openToast', payload);
      setTimeout(() => {
        commit('closeToast')
      }, 2000)
    }
  },

  modules: {
  }
})
